package org.chengyi.model_shop.service.impl;

import org.chengyi.model_shop.entity.Category;
import org.chengyi.model_shop.entity.CategoryDetail;
import org.chengyi.model_shop.mapper.CategoryMapper;
import org.chengyi.model_shop.mapper.ProductMapper;
import org.chengyi.model_shop.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<CategoryDetail> getCategoryList() {
        List<CategoryDetail> categoryList = categoryMapper.getCategoryList();
        System.out.println(categoryList);
        return categoryList;
    }

    @Override
    public String addCategory(Category category) {
        if (categoryMapper.getCategoryList().contains(category.getName())) {
            return "该分类已存在";
        }
        if (category.getName() == null){
            return "分类名称不能为空";
        }
        return categoryMapper.addCategory(category) > 0 ? "添加成功" : "添加失败";
    }

    @Override
    public String deleteCategory(Integer categoryId) {
        if (!categoryMapper.getById(categoryId)) {
            return "该分类不存在";
        }
        if (productMapper.getProductListByCategory(categoryId).size() > 0) {
            return "该分类下有商品，请先删除该分类下的商品";
        }
        return categoryMapper.deleteCategory(categoryId) > 0 ? "删除成功" : "删除失败";
    }

    @Override
    public String updateCategory(Category category) {
        if (category.getName() == null){
            return "分类名称不能为空";
        }
        return categoryMapper.updateCategory(category) > 0 ? "修改成功" : "修改失败";
    }

}
